<template>
  <j-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开始时间：">
          <a-date-picker format="YYYY-MM-DD HH:mm:ss" :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                         v-decorator="[ 'startTime', validatorRules.startTime]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="截至时间：">
          <a-date-picker format="YYYY-MM-DD HH:mm:ss" :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                         v-decorator="[ 'endTime', validatorRules.endTime]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="关联经销商">
          <a-select v-if="isAdd"
            mode="multiple"
            :placeholder="'请选择经销商'"
            v-model="selectStoreValue"
            :options="selectStoreOptions"
            :disabled="false"
            :open="false"
            style="width: 80%;"
          />
          <a-select v-else
                    mode="multiple"
                    :placeholder="'请选择经销商'"
                    v-model="selectStoreValue"
                    :options="selectStoreOptions"
                    :disabled="true"
                    :open="false"
                    style="width: 80%;"
          />
          <a-button v-show="isAdd" type="primary" icon="search" @click="handleShowStore">选择经销商</a-button>
          <selectStoreModalV2 ref="SelectStoreModalV2" @selectFinished="selectStoreOK"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="货物优惠">
          <a-input-number style="width: 200px;" v-decorator="[ 'productAmount', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="促销费用">
          <a-input-number style="width: 200px;" v-decorator="[ 'promotionAmount', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="任务完成">
          <a-input-number style="width: 200px;" v-decorator="[ 'missionAmount', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="调价补差">
          <a-input-number style="width: 200px;" v-decorator="[ 'adjustAmount', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import {httpAction} from '@/api/manage'
import JDate from '@/components/jeecg/JDate'
import pick from 'lodash.pick'
import moment from "moment"
import SelectStoreModalV2 from '../../stm/modules/SelectStoreModalV2'
import {getAction, postAction, putAction} from "../../../api/manage";

export default {
  name: 'StmDealerDiscountModal',
  components: {
    JDate,
    SelectStoreModalV2
  },
  data() {
    return {
      title: "操作",
      visible: false,
      selectStoreValue: [],
      ids: '',
      isAdd: false,
      selectStoreOptions: [],
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {},
      url: {
        add: "/stm/stmDealerDiscount/add",
        edit: "/stm/stmDealerDiscount/edit",
        storeList: "/stm/stmStoreInfo/storeListByStoreIds",
      }
    }
  },
  methods: {
    moment,
    add() {

      this.selectStoreValue = [];
      this.selectStoreOptions = [];
      this.edit({},'');
    },
    edit(record, ids, storeIds) {
      this.isAdd=false;
      if(ids===''){
        this.isAdd=true;
      }

      this.ids=ids;
      this.form.resetFields();
      this.model = Object.assign({}, record);
      //初始化明细表数据
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'startTime', 'endTime', 'storeId', 'companyCode', 'channelCode', 'productAmount', 'productRemainAmount', 'promotionAmount', 'promotionRemainAmount', 'missionAmount', 'missionRemainAmount', 'adjustAmount', 'adjustRemainAmount', 'auditStatus', 'auditBy', 'auditTime',))
        // 时间格式化
        this.form.setFieldsValue({startTime: this.model.startTime ? moment(this.model.startTime) : null})
        this.form.setFieldsValue({endTime: this.model.endTime ? moment(this.model.endTime) : null})

      });
      //编辑时获取门店
      if(!this.isAdd){
        this.initStore(storeIds)
      }
    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';

          let formData = Object.assign(this.model, values);
          //时间格式化
          formData.startTime = formData.startTime ? formData.startTime.format('YYYY-MM-DD HH:mm:ss') : null;
          formData.endTime = formData.endTime ? formData.endTime.format('YYYY-MM-DD HH:mm:ss') : null;

          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
            formData.storeIdList=this.selectStoreValue;
          } else {
            httpurl += this.url.edit;
            method = 'put';
            formData.ids=this.ids;
          }
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
              that.confirmLoading = false;
              that.close();
            } else {
              that.$message.warning(res.message);
              that.confirmLoading = false;
            }
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    handleShowStore: function () {
      this.$refs.SelectStoreModalV2.add();
      this.$refs.SelectStoreModalV2.title = "选择经销商";
    },
    selectStoreOK(storeData) {
      this.selectStoreOptions = storeData.map(data => ({label: data['name'], value: data['storeId']}));
      this.selectStoreValue = storeData.map(data => data['storeId']);

    },
    initStore(storeIds) {
      console.log(storeIds)
      let param = {};
      param.storeIdList = storeIds;
      getAction(this.url.storeList, param).then((res) => {
        if (res.success) {
          this.selectStoreOptions = res.result.map(data => ({label: data['name'], value: data['storeId']}));
          this.selectStoreValue = res.result.map(data => data['storeId']);
        } else {
          this.$message.warning(res.message);
        }
      })
    }
  }
}
</script>

<style scoped>
.ant-btn {
  padding: 0 10px;
  margin-left: 3px;
}

.ant-form-item-control {
  line-height: 0px;
}

/** 主表单行间距 */
.ant-form .ant-form-item {
  margin-bottom: 10px;
}

/** Tab页面行间距 */
.ant-tabs-content .ant-form-item {
  margin-bottom: 0px;
}
</style>